<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Models\Admin as AdminInfo;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;

class Admin extends Controller
{
    //管理员列表
    public function all(){
        $adminInfo = (new AdminInfo)->orderBy('is_supper','asc')->orderBy('status')->paginate(5);
        // $viewData = [
        //     'admins' => $adminInfo,
        // ];
        $data = [
            'admins' => $adminInfo,
        ];
        return view('admin.admin.all',$data);
    }

    //管理员添加
    public function add(){
        if (request()->isMethod('post')) {
            $data = request()->only(['username','password','nickname','email']);
            $result = (new AdminInfo)->add($data);
            if ($result == 1) {
                $msg = [
                    'code' => 1,
                    'msg' => "添加成功",
                    'url' => url('admin/adminlist'),
                ];
            } else {
                $msg = [
                    'code' => 0,
                    'msg' => $result,
                ];
            }
            return $msg;
        }
        return view('admin.admin.add');
    }

    //管理员删除
    public function del()
    {
       
            
            $admins = AdminInfo::find(request('id'));   
            $result = $admins->delete();
            if ($result) {
                $msg = [
                    'code' => 1,
                    'msg' => "删除成功",
                    'url' => url('admin/adminlist'),
                ];
            } else {
                $msg = [
                    'code' => 0,
                    'msg' => "删除失败",
                ];    
            }
            return $msg;
        
    }

    //管理员状态
    public function status()
    {
        //$data = request()->only(['id','status']);
        $adminInfo = AdminInfo::find(request('id'));
        $adminInfo->status = request('status') ? '0' : 1;
        $result = $adminInfo->save();
        if ($result) {
            $msg = [
                'code' => 1,
                'msg' => "操作成功",
                'url' => url('admin/adminlist')
            ];
        } else {
            $msg = [
                'code' => 0,
                'msg' => "操作失败",
            ];
        }
        return $msg;
    }

    //管理员编辑
    public function edit ()
    {
        if (request()->isMethod('post')) {
            $data = request()->only(['id','username','password','nickname','email']);
            $result = (new AdminInfo)->edit($data);
            if ($result == 1) {
               $msg = [
                   'code' => 1,
                   'msg' => '编辑成功',
                   'url' => url('admin/adminlist'),
               ];
            } else {
                $msg = [
                    'code' => 0,
                    'msg' => $result,
                ];
            }
            return $msg;
        }
       
        $id = request()->route( 'id' ); 
        $adminInfo = AdminInfo::find($id);
        
        $data = [
            'admins' => $adminInfo,
        ];
        return view('admin.admin.edit',$data);
    }
}
